/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.alcj.service.impl;

import com.alcj.common.utils.StringUtils;
import com.alcj.mapper.CarDataMapper;
import com.alcj.model.CarData;
import com.alcj.service.CarDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 车辆资料Service
 * @author wdl
 * @version 2017-10-19
 */
@Service
public class CarDataServiceImpl  implements CarDataService {

	@Autowired
	private CarDataMapper cardao;

	@Override
	public CarData get(String id) {
		return cardao.get(id);
	}

	@Override
	public CarData get(CarData entity) {
		return cardao.get(entity);
	}


	@Override
	public List<CarData> findList(CarData carData) {
		List<CarData> list=cardao.findList(carData);
		if(list.size()>0){
          for (CarData demo:list){
            if(StringUtils.isNotBlank(demo.getImgUrl()) && demo.getImgUrl().indexOf("D:")!=-1 ){
				demo.setImgUrl( demo.getImgUrl().replace("D:",""));
			}
		  }
		}
		return list;
	}

	@Override
	public List<CarData> findListById(CarData entity){
		return cardao.findListById(entity);
	};

	@Override
	public List<CarData> findAllList(CarData entity) {
		return cardao.findAllList(entity);
	}

	@Override
	public List<CarData> findAllList() {
		return cardao.findAllList();
	}

	@Override
	public int insert(CarData entity) {
		//String userid="0";//测试 获取当前用户ID
        //整个需要放开的代码
		String userid=entity.getUser().getUserid();
		if (StringUtils.isBlank(entity.getId())){
			entity.preInsert(userid);
			int returnInt=cardao.insert(entity);
			Map map=new HashMap();
			map.put("carId",entity.getId());
			map.put("userId",userid);
			cardao.insertCarUser(map);
			return returnInt;
		}else{
			// 更新用户数据
			entity.preUpdate(userid);
			return	cardao.update(entity);
		}
	}

	@Override
	public int update(CarData entity) {
		return cardao.update(entity);
	}

	@Override
	public int delete(String id) {
		return delete(id);
	}

	@Override
	public int  deleteCarUser(CarData carData) {
		return cardao.deleteCarUser(carData);
	}

	/**
	 *插入车辆 和 人关系
	 * @param  (#{userId},#{carId})
	 * @return
	 */
	@Override
	public void insertCarUser(Map map){
		cardao.insertCarUser(map);
	}

	@Override
	public CarData validatingCode(CarData carData) {
		return cardao.get(carData);
	}
}